<?php
class PhanloaisvsController extends AppController {

	var $name = 'Phanloaisvs';
	var $uses = array('Phanloaisv','Xeploai','Sinhvien','Chidoansv','Namhoc','Hockie','Thanhtichsv','Thanhtichkhoasv','Z5plsv','Z1dvutsv','Xeploaicd','Loaicdsv');
	var $helpers=array('FlashChart');
    	    
    /**
     * function _loaiSinhVien()
     * Phan loai doan vien sinh vien dua vao table Xeploais with input (diemrenluyen,diemtbhk)
     * Ket qua tra ve la id cua table Xeploais
     * id	=> 	xeploai
     * 1	=>	Xuat sac
     * 2	=>	Kha
     * 3	=>	Trung binh
     * 4	=>	Yeu
     * Rang buoc 0<=diemrenluyen<=100, 0<=diemtbhk<=100 da thuc hien o buoc nhap nen o day khong co dieu kien rang buoc giup cho code "clean and clear"
     */
    function _loaiSinhVien($diemrenluyen,$diemtbhk){
        $kq = $this->Xeploai->find('all');
        if($diemrenluyen>=$kq[0]['Xeploai']['diemrenluyen'] && $diemtbhk>=$kq[0]['Xeploai']['diemtbhk'])
        	$xeploai = $kq[0]['Xeploai']['id'];//Xuat sac
        	
        	else if($diemrenluyen>=$kq[1]['Xeploai']['diemrenluyen'] && $diemtbhk>=$kq[1]['Xeploai']['diemtbhk'])
        		$xeploai = $kq[1]['Xeploai']['id'];//Kha 
        		
        		else if($diemrenluyen>=$kq[2]['Xeploai']['diemrenluyen'] && $diemtbhk>=$kq[2]['Xeploai']['diemtbhk'])
        			$xeploai = $kq[2]['Xeploai']['id'];//Trung binh
        			
        			else if($diemrenluyen>=$kq[3]['Xeploai']['diemrenluyen'] && $diemtbhk>=$kq[3]['Xeploai']['diemtbhk'])
        				$xeploai = $kq[3]['Xeploai']['id'];//Yeu
        					
        					else $xeploai = "Something wrong";//Khac cac truong hop tren se thong bao loi
        return $xeploai;
    }
    
	/**
     * function phanloai()
     * Phan loai toan bo doan vien 
     * Vi chua co user nen khong the xac dinh duoc chi doan nao de phan loai tung chi doan
     * */    
	function phanloai(){
        $hk=$this->getHocKyHt();
        //Lay danh sach sinh vien can phan loai || Can phai them dieu kien de xet theo chi doan.
		$dsphanloai = $this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$this->Session->read('chidoanht_id'))));
        //pr($dsphanloai);
		foreach($dsphanloai as $phanloai){
		  if($phanloai['Z5plsv']['duyet']==0){
			$xeploai=$this->_loaiSinhVien($phanloai['Z5plsv']['diemrenluyen'],$phanloai['Z5plsv']['diemtbhk']);
		 	$sinhvien_id=$phanloai['Z5plsv']['sinhvien_id'];
		 	$this->Phanloaisv->updateAll(
    			array('Phanloaisv.xeploai_id' => $xeploai),
    			array('Phanloaisv.sinhvien_id ' => $sinhvien_id)
			);
           }
		}
        $this->redirect(array('action' => 'index'));
		//pr($this->_loaiSinhVien(100,3.29));//edit value _loaiSinhVien(diemrenluyen,diemtbhk);
	}  
     
    /**
     *     	Liet ke danh sanh cua doan vien trong cung 1 hoc ki	
     *     	Table hockies de lay ngay bat dau va ngay ket thuc
     *     	Table phong
     *     	Tim hockies dua tren dk hocki_id nen se tra ve 1 hocki duy nhat
     *     	Khi dua tham so hocki_id vao se tim duoc thoi gian bat dau va ket thuc, dua vao do lay ngay ket thuc cua phong trao (thanhtichsv) de liet ke danh sach phong trao thoa dk
     *     	$hk_id = '1';//Hocki_id la tham so duoc truyen tu ngoai vao ham
     *     	$mssv = '1081636';
     */
	function thanhtichSV($mssv,$hk_id){
       	//$hk_id = '1';//Hocki_id la tham so duoc truyen tu ngoai vao ham
       	//$mssv = '1081636';
		//Ngay bat dau Ngay ket thuc hoc ki	
		$hocki = $this->Hockie->find('all', array('conditions' => array('Hockie.id' => $hk_id)));
		$x = $hocki[0]['Hockie']['ngaybatdau'];
		$y = $hocki[0]['Hockie']['ngayketthuc'];
		
		//Thanhtichsv
		$thanhtich = $this->Thanhtichsv->find('all', array('conditions' => array(
																				'Sinhvien.id' => $mssv,
																				array('Phongtraosv.ngayketthuc BETWEEN ? AND ?' => array($x,$y))
													)));
		return count($thanhtich);
        //pr($thanhtich);
	}    
	
	/**
		Nhap vao MSSV va HOCKI_ID tra ve so thanh tich trong hoc ky cua mot sv khi biet duoc mssv va hocky.
        mssv: nhap vao
        hoc ky: lay id tu hoc ky hien tai.
	*/
	function _thanhtichKhoasv($mssv,$hk_id){
	   
        $hocki = $this->Hockie->find('all', array('conditions' => array('Hockie.id' => $hk_id)));
        $x = $hocki[0]['Hockie']['ngaybatdau'];
        $y = $hocki[0]['Hockie']['ngayketthuc'];
        $thanhtichkhoa = $this->Thanhtichkhoasv->find('all', array('conditions' => array(
        																'Sinhvien.id' => $mssv,
        																array('Phongtraokhoa.ngayketthuc BETWEEN ? AND ?' => array($x,$y))
                                                      )));
        return count($thanhtichkhoa);
	}  
    
	/**
     * function index()
     * Loc ra danh sach sinhvien co cung ma lop voi bi thu, va trong mot học ky xac dinh
     * */    
	function index() {
		$this->Z5plsv->recursive = 0;
        $xeploais = $this->Xeploai->find('list',array('fields'=>'tenloai'));
        $this->set('xeploais', $xeploais);
        $hk=$this->getHocKyHt();
        $chidoan_id=$this->Session->read('chidoanht_id');
        $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$chidoan_id)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisvs as $phanloaisv){
            if($phanloaisv['Z5plsv']['xeploai_id']==1)$xs++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==2) $k++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==3) $tb++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==4)$y++;
        }
        $tong=count($phanloaisvs);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $loaicdsv=$this->Xeploaicd->find('list',array('fields'=>'loaicd'));
        $loaicd=$this->__phanloaicd($tlxs,$tlk,$tltb,$tly);
        $check=$this->Loaicdsv->find('first',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$chidoan_id),'recursive'=>-1));
        //pr($check);
        if($check==null){
            $this->Loaicdsv->saveAll(array('xeploaicd_id'=>$loaicd,'hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$chidoan_id));    
        }else $loaicd=$check['Loaicdsv']['xeploaicd_id'];
        $this->set(compact('phanloaisvs','tlxs','tlk','tltb','tly','tong','loaicd','loaicdsv'));
        $this->loadModel('Thoigianchophep');
        $thoigian=$this->Thoigianchophep->find('first',array('conditions'=>array('id'=>1)));
        $this->set('thoigian',$thoigian);
	}
    
    /**
     * function capnhat()
     * Update ket qua cap nhat sau khi bi thu check lai lan cuoi
     * */
    function capnhat(){
        //pr($this->data);exit;
        
            for($j=1;$j<count($this->data['Phanloaisv'])/2;$j++){
            //echo $this->data['Phanloaisv']['xeploai_id'.$j];
                if($this->data['Phanloaisv']['xeploai_id'.$j]!=null&&isset($this->data['Phanloaisv']['xeploai_id'.$j])){
                    $this->Phanloaisv->updateAll(
            			array('Phanloaisv.xeploai_id' => $this->data['Phanloaisv']['xeploai_id'.$j]),
            			array('Phanloaisv.sinhvien_id ' => $this->data['Phanloaisv']['sinhvien_id'.$j], 'Phanloaisv.duyet'=>0)
        			);
                }else {}//thong bao loi
            }
        //}else {}//thong bao loi
        
        $this->redirect(array('action' => 'index'));
    }
    
    /**
     * function __phanloaicd()
     * Get cac gia tri % doan vien xs, tb, y, kha de xep loai chi doan do
     * luu ket qua xep loai vao bang loaicdsvs
     * */   
     function __phanloaicd($tlxs,$tlk,$tltb,$tly){
        $xeploaicd=$this->Xeploaicd->find('all');        
        //pr($xeploaicd);
        $data=$this->data['Phanloaisv'];
        //pr($data);
        $xeploaivm=$xeploaicd[0]['Xeploaicd'];
        $xeploaik=$xeploaicd[1]['Xeploaicd'];
        $xeploaitb=$xeploaicd[2]['Xeploaicd'];
        $xeploaiy=$xeploaicd[3]['Xeploaicd'];
        $loaicd=0;
        if(($tlxs>=$xeploaiy['xs1']&&$tlxs<=$xeploaiy['xs2'])&&($tlk>=$xeploaiy['k1']&&$tlk<=$xeploaiy['k2'])&&($tltb>=$xeploaiy['tb1']&&$tltb<=$xeploaiy['tb2'])&&($tly>=$xeploaiy['y1']&&$tly<=$xeploaiy['y2'])){
            $loaicd=4;//'Chi đoàn yếu kém';        
        }else if(($tlxs>=$xeploaitb['xs1']&&$tlxs<=$xeploaitb['xs2'])&&($tlk>=$xeploaitb['k1']&&$tlk<=$xeploaitb['k2'])&&($tltb>=$xeploaitb['tb1']&&$tltb<=$xeploaitb['tb2'])&&($tly>=$xeploaitb['y1']&&$tly<=$xeploaitb['y2'])){
            $loaicd=3;//'Chi đoàn trung bình';
        }else if(($tlxs>=$xeploaik['xs1']&&$tlxs<=$xeploaik['xs2'])&&($tlk>=$xeploaik['k1']&&$tlk<=$xeploaik['k2'])&&($tltb>=$xeploaik['tb1']&&$tltb<=$xeploaik['tb2'])&&($tly>=$xeploaik['y1']&&$tly<=$xeploaik['y2'])){ 
            $loaicd=2;//'Chi đoàn khá';
        }else if(($tlxs>=$xeploaivm['xs1']&&$tlxs<=$xeploaivm['xs2'])&&($tlk>=$xeploaivm['k1']&&$tlk<=$xeploaivm['k2'])&&($tltb>=$xeploaivm['tb1']&&$tltb<=$xeploaivm['tb2'])&&($tly>=$xeploaivm['y1']&&$tly<=$xeploaivm['y2'])){
            $loaicd=1;//'Chi đoàn vững mạnh';
        }else $loaicd=1;//'Chi đoàn vững mạnh';
        return $loaicd;
     }
     
     /**
     * function danhsachcd()
     * Liet ke danh sach cac chi doan de duyet
     * */   
    function danhsachcd(){
        $khoa_id=$this->Session->read('khoa_id');
        $chidoansvs=$this->Chidoansv->find('all',array('conditions'=>array('khoahoc_id <>'=>null,'khoa_id'=>$khoa_id)));
        $this->set(compact('chidoansvs'));
        //$model='Chidoansv';
//        if(!empty($this->data)){
//            if(trim($this->data['Phanloaisv']['data'])!=''){
//                $results=$this->$model->find('all',array(
//                                            'conditions'=>array(
//                                                            'or'=>array(
//                                                                $model.'.machidoansv Like '=>'%'.$this->data['Phanloaisv']['data']."%",
//                                                                $model.'.tenchidoan Like '=>'%'.$this->data['Phanloaisv']['data'].'%'
//                ))));
//                if(!empty($results)){
//                    $this->set('results', $results);    
//                }else {$this->set('check', "No results!");}
//                
//            }
//        }
    }
    /**
     * function duyetcd($cdid=null)
     * The hien form pldv cua chi doan dang duyet
     * $cdid la id cua chi doan dang duyet
     * */ 
    function duyetcd($cdid=null){
        $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
        $this->set('chidoan',$chidoan);
        $this->Z5plsv->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        $hk=$this->getHocKyHt();
        //$chidoan_id=$this->Session->read('chidoanht_id');
        $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisvs as $phanloaisv){
            if($phanloaisv['Z5plsv']['xeploai_id']==1)$xs++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==2) $k++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==3) $tb++;
             else $y++;
        }
        $tong=count($phanloaisvs);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        //$loaicdsv=$this->Xeploaicd->find('list',array('fields'=>'loaicd'));
        $loaicdsv=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd'),'recursive'=>-1));
        //$loaicd=$this->__phanloaicd($tlxs,$tlk,$tltb,$tly);
        $check=$this->Loaicdsv->find('first',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid),'recursive'=>-1));
        //pr($check);
        $loaicd=$check['Loaicdsv']['xeploaicd_id'];
        $hockies=$this->Hocky->find('list',array('fields'=>array('tenhocky'),'recursive'=>-1));
        $this->set(compact('phanloaisvs','tlxs','tlk','tltb','tly','tong','loaicd','loaicdsv','hockies'));  
        
        //duyet doan vien uu tu
        $hk=$this->getHocKyHt();
       $dvutsvs=$this->Z1dvutsv->find('all', array('conditions'=> array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));
       $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
       $this->set(compact('dvutsvs','chidoan'));      
    }
    function thongke($hocky=null,$cdid=null){
        $this->layout='search';
        $hk=$this->Hocky->find('first',array('conditions'=>array('tenhocky'=>$hocky),'recursive'=>-1));//pr($hk);exit;
        $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));
        $this->set(compact('phanloaisvs','hocky'));
        $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
        $this->set('chidoan',$chidoan);
        $this->Z5plsv->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        //$chidoan_id=$this->Session->read('chidoanht_id');
        $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisvs as $phanloaisv){
            if($phanloaisv['Z5plsv']['xeploai_id']==1)$xs++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==2) $k++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==3) $tb++;
             else $y++;
        }
        $tong=count($phanloaisvs);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $loaicdsv=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd'),'recursive'=>-1));
        $check=$this->Loaicdsv->find('first',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid),'recursive'=>-1));        
        $loaicd=$check['Loaicdsv']['xeploaicd_id'];
        $hockies=$this->Hocky->find('all',array('fields'=>array('tenhocky'),'recursive'=>-1));
        $dvutsvs=$this->Z1dvutsv->find('all', array('conditions'=> array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));
        $this->set(compact('tlxs','tlk','tltb','tly','tong','loaicd','loaicdsv','hockies','dvutsvs'));
    }
    /**
     * function duyet()
     * Duyet 1 hoac nhiu sinh vien cung luc
     * Co 2 chuc nang: duyet, mo khoa
     * */ 
    function duyet(){
        $hk=$this->getHocKyHt();
        $cd=$this->data['Phanloaisv']['chidoan_id'];
        //pr($this->data);exit;
        $chuaduyetids = array();   
        if(isset($this->data['Phanloaisv']['chuaduyet']))
        {           
		foreach($this->data['Phanloaisv']['chuaduyet'] as $id => $value){
			if($value == 1){
	  		  $chuaduyetids[] = $id;
			}
		}
        }
        $duyetids = array();
        if(isset($this->data['Phanloaisv']['duyet']))
        {
        foreach($this->data['Phanloaisv']['duyet'] as $id => $value){
			if($value == 1){
	  		  $duyetids[] = $id;
			}
		}
        }
        //pr($chuaduyetids);exit;
		if (count($duyetids) == 0 && count($chuaduyetids) == 0  ) {
			$this->Session->setFlash(__('Khong co sinh vien nao duoc chon', true));
			$this->redirect(array('action'=>'duyetcd',$cd));
		}else {
            $this->Loaicdsv->updateAll(array('Loaicdsv.duyet'=>1),array('Loaicdsv.chidoansv_id'=>$cd,'Loaicdsv.hocky_id'=>$hk['Hocky']['id']));
    		$this->Phanloaisv->updateAll(array('Phanloaisv.duyet'=>1),array('Phanloaisv.sinhvien_id'=>$duyetids));
            $this->Phanloaisv->updateAll(array('Phanloaisv.duyet'=>0),array('Phanloaisv.sinhvien_id'=>$chuaduyetids));
			$this->Session->setFlash(__('Đã lưu !', true),'success');
			$this->redirect(array('action'=>'duyetcd',$cd));	
		}
    }
    function reviewkqpldv(){
        $cdid=$this->Session->read('chidoanht_id');
        $this->Z5plsv->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        $hk=$this->getHocKyHt();
        $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisvs as $phanloaisv){
            if($phanloaisv['Z5plsv']['xeploai_id']==1)$xs++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==2) $k++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==3) $tb++;
             else $y++;
        }
        $tong=count($phanloaisvs);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $dvutsvs=$this->Z1dvutsv->find('all', array('conditions'=> array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));        
        $loaicdsv=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd'),'recursive'=>-1));
        $check=$this->Loaicdsv->find('first',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid),'recursive'=>-1));
        $loaicd=$check['Loaicdsv']['xeploaicd_id'];
        $this->set(compact('phanloaisvs','tlxs','tlk','tltb','tly','tong','loaicd','loaicdsv','dvutsvs'));        
    }
    function ketquapldv(){
        $this->layout='excel';
        $cdid=$this->Session->read('chidoanht_id');
        $this->Z5plsv->recursive = -1;
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $this->set('xeploais', $xeploais);
        $hk=$this->getHocKyHt();
        $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));
        $tb=0;$y=0;$xs=0;$k=0;
        foreach($phanloaisvs as $phanloaisv){
            if($phanloaisv['Z5plsv']['xeploai_id']==1)$xs++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==2) $k++;
             else if($phanloaisv['Z5plsv']['xeploai_id']==3) $tb++;
             else $y++;
        }
        $tong=count($phanloaisvs);
        if($tong>0){
            $tlxs=($xs/$tong)*100;
            $tlk=($k/$tong)*100;
            $tltb=($tb/$tong)*100;
            $tly=($y/$tong)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $dvutsvs=$this->Z1dvutsv->find('all', array('conditions'=> array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));        
        $loaicdsv=$this->Xeploaicd->find('all',array('fields'=>array('id','loaicd'),'recursive'=>-1));
        $check=$this->Loaicdsv->find('first',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid),'recursive'=>-1));
        $loaicd=$check['Loaicdsv']['xeploaicd_id'];
        $this->set(compact('phanloaisvs','tlxs','tlk','tltb','tly','tong','loaicd','loaicdsv','dvutsvs'));        
    }
    function reviewall(){
        $hk=$this->getHocKyHt();
        $khoa_id=$this->Session->read('khoa_id');
        $namhoc_id=$this->Session->read('namhocht_id');
        $xs=0;$k=0;$tb=0;$y=0;
        $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'])));
        foreach($phanloaisvs as $phanloaisv){
            if($phanloaisv['Z5plsv']['xeploai_id']==1) $xs++;
            else if($phanloaisv['Z5plsv']['xeploai_id']==2) $k++;
                else if($phanloaisv['Z5plsv']['xeploai_id']==3) $tb++;
                    else if($phanloaisv['Z5plsv']['xeploai_id']==4) $y++;
        }
        $this->loadModel('Z1plcb');
        $phanloaicbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id)));
        //$cbxs=0;$cbk=0;$cbtb=0;$cby=0;
        foreach($phanloaicbs as $phanloaicb){
            if($phanloaicb['Z1plcb']['xeploai_id']==1) $xs++;
            else if($phanloaicb['Z1plcb']['xeploai_id']==2) $k++;
                else if($phanloaicb['Z1plcb']['xeploai_id']==3) $tb++;
                    else if($phanloaicb['Z1plcb']['xeploai_id']==4) $y++;
        }
        $tong=count($phanloaisvs)+count($phanloaicbs);
        if($tong>0){
            $tlxs=round($xs/$tong,2)*100;
            $tlk=round($k/$tong,2)*100;
            $tltb=round($tb/$tong,2)*100;
            $tly=round($y/$tong,2)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $this->loadModel('Z1dvutsv');
        $dvutsvs=$this->Z1dvutsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id']),'recursive'=>0));
        $this->loadModel('Z1dvutcb');
        $dvutcbs=$this->Z1dvutcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id),'recursive'=>0));
        $tongdvut=count($dvutcbs)+count($dvutsvs);
        $chidoans=$this->Chidoansv->find('all',array('conditions'=>array('khoa_id'=>$khoa_id)));
        $chidoancbs=$this->Chidoansv->find('all',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id'=>null)));
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $loaicdsvs=$this->Loaicdsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'])));
        $loaicdcbs=$this->Loaicdsv->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id)));
        //pr($loaicdsvs);
        //pr($phanloaisvs);
        $this->set(compact('phanloaicbs','phanloaisvs','chidoans','chidoancbs','xeploais','loaicdsvs','loaicdcbs','tlxs','tlk','tltb','tly','tongdvut'));
    }
    function ketquapldvall(){
        $this->layout = 'excel';
        $hk=$this->getHocKyHt();
        $khoa_id=$this->Session->read('khoa_id');
        $namhoc_id=$this->Session->read('namhocht_id');
        $xs=0;$k=0;$tb=0;$y=0;
        $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'])));
        foreach($phanloaisvs as $phanloaisv){
            if($phanloaisv['Z5plsv']['xeploai_id']==1) $xs++;
            else if($phanloaisv['Z5plsv']['xeploai_id']==2) $k++;
                else if($phanloaisv['Z5plsv']['xeploai_id']==3) $tb++;
                    else if($phanloaisv['Z5plsv']['xeploai_id']==4) $y++;
        }
        $this->loadModel('Z1plcb');
        $phanloaicbs=$this->Z1plcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id)));
        //$cbxs=0;$cbk=0;$cbtb=0;$cby=0;
        foreach($phanloaicbs as $phanloaicb){
            if($phanloaicb['Z1plcb']['xeploai_id']==1) $xs++;
            else if($phanloaicb['Z1plcb']['xeploai_id']==2) $k++;
                else if($phanloaicb['Z1plcb']['xeploai_id']==3) $tb++;
                    else if($phanloaicb['Z1plcb']['xeploai_id']==4) $y++;
        }
        $tong=count($phanloaisvs)+count($phanloaicbs);
        if($tong>0){
            $tlxs=round($xs/$tong,2)*100;
            $tlk=round($k/$tong,2)*100;
            $tltb=round($tb/$tong,2)*100;
            $tly=round($y/$tong,2)*100;    
        }else {
            $tlxs=0;
            $tlk=0;
            $tltb=0;
            $tly=0;
        }
        $this->loadModel('Z1dvutsv');
        $dvutsvs=$this->Z1dvutsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id']),'recursive'=>0));
        $this->loadModel('Z1dvutcb');
        $dvutcbs=$this->Z1dvutcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id),'recursive'=>0));
        $tongdvut=count($dvutcbs)+count($dvutsvs);
        $chidoans=$this->Chidoansv->find('all',array('conditions'=>array('khoa_id'=>$khoa_id)));
        $chidoancbs=$this->Chidoansv->find('all',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id'=>null)));
        $xeploais = $this->Xeploai->find('all',array('fields'=>array('id','tenloai'),'recursive'=>-1));
        $loaicdsvs=$this->Loaicdsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id'])));
        $loaicdcbs=$this->Loaicdsv->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id)));
        //pr($loaicdsvs);
        //pr($phanloaisvs);
        $this->set(compact('phanloaicbs','phanloaisvs','chidoans','chidoancbs','xeploais','loaicdsvs','loaicdcbs','tlxs','tlk','tltb','tly','tongdvut'));
    }
    function tilepldv($namhoc_id=0,$hocky_id = 0)
    {
    	$khoaht_id=$this->Session->read('khoa_id');
        $chidoanht=$this->Session->read('chidoanht_id');
        $info=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$chidoanht),'recursive'=>-1));
        if(!empty($this->data))
        {
        	$namhoc_id =$this->data['Phanloaisv']['namhoc'] ;
        	$hocky_id =$this->data['Phanloaisv']['hocky'] ;
        	$this->redirect(array('action'=>'tilepldv',$namhoc_id,$hocky_id));
        }
         if($namhoc_id==0)
			$namhoc_id = $this->Session->read('namhocht_id');
    	if($hocky_id==0)
        {
        	$hk=$this->getHocKyHt();
        	$hocky_id = $hk['Hocky']['id'];
        }
        if($khoaht_id==$info['Chidoansv']['khoa_id']){
            $this->Z5plsv->recursive = -1;
            $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hocky_id)));
            $tb=0;$y=0;$xs=0;$k=0;
            foreach($phanloaisvs as $phanloaisv){
                if($phanloaisv['Z5plsv']['xeploai_id']==1)$xs++;
                 else if($phanloaisv['Z5plsv']['xeploai_id']==2) $k++;
                 else if($phanloaisv['Z5plsv']['xeploai_id']==3) $tb++;
                 else $y++;
            }
            $tong=count($phanloaisvs);
            if($tong>0){
                $tlxs=round(($xs/$tong),2)*100;
                $tlk=round(($k/$tong),2)*100;
                $tltb=round(($tb/$tong),2)*100;
                $tly=round(($y/$tong),2)*100;    
            }else {
                $tlxs=0;
                $tlk=0;
                $tltb=0;
                $tly=0;
            }
            $this->Namhoc->displayField='namhoc';
    		$namhocs = $this->Namhoc->find('list');
    		$hockies = $this->Hocky->find('list',array('conditions'=>array('namhoc_id'=>$namhoc_id),'fields'=>array('tenhocky')));
            $tyles = array(array('loai'=>'Xuất sắc','tyle'=>$tlxs),array('loai'=>'Khá','tyle'=>$tlk),array('loai'=>'Trung bình','tyle'=>$tltb),array('loai'=>'Yếu','tyle'=>$tly));
            $this->set(compact('tyles','tong','namhocs','hockies','namhoc_id','hocky_id'));   
        }else{
            $this->Session->setFlash(__('Invalid! Khong thuoc khoa cua ban', true));
        }
             
    }
    
    function tiledrl($namhoc_id=0,$hocky_id = 0)
    {
        $khoaht_id=$this->Session->read('khoa_id');
        $chidoanht=$this->Session->read('chidoanht_id');
        $info=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$chidoanht),'recursive'=>-1));
        if(!empty($this->data))
        {
        	$namhoc_id =$this->data['Phanloaisv']['namhoc'] ;
        	$hocky_id =$this->data['Phanloaisv']['hocky'] ;
        	$this->redirect(array('action'=>'tiledrl',$namhoc_id,$hocky_id));
        }
        if($khoaht_id==$info['Chidoansv']['khoa_id']){
            $this->Z5plsv->recursive = -1;
            if($namhoc_id==0)
    			$namhoc_id = $this->Session->read('namhocht_id');
            if($hocky_id==0)
            {
            	$hk=$this->getHocKyHt();
            	$hocky_id = $hk['Hocky']['id'];
            }
            $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hocky_id)));
            
            $tb=0;$y=0;$xs=0;$k=0;$g=0;
            foreach($phanloaisvs as $phanloaisv){
                if($phanloaisv['Z5plsv']['diemrenluyen']<=60)$y++;
                 else if($phanloaisv['Z5plsv']['diemrenluyen']<=70) $k++;
                 else if($phanloaisv['Z5plsv']['diemrenluyen']<=80) $g++;
                 else $xs++;
            }
            $tong=count($phanloaisvs);
            if($tong>0){
                $tlxs=(round($xs/$tong,2)*100);
                $tlk=(round($k/$tong,2)*100);
                $tltb=(round($tb/$tong,2)*100);
                $tly=(round($y/$tong,2)*100);
                $tlg=(round($g/$tong,2)*100);      
            }else
            {
                $tlxs=0;
                $tlk=0;
                $tltb=0;
                $tly=0;
                $tlg=0;
            }
            $this->Namhoc->displayField='namhoc';
    		$namhocs = $this->Namhoc->find('list');
    		$hockies = $this->Hocky->find('list',array('conditions'=>array('namhoc_id'=>$namhoc_id),'fields'=>array('tenhocky')));
            $tyles = array(array('loai'=>'Xuất sắc','tyle'=>$tlxs),array('loai'=>'Giỏi','tyle'=>$tlg),array('loai'=>'Khá','tyle'=>$tlk),array('loai'=>'Trung bình','tyle'=>$tltb),array('loai'=>'Yếu','tyle'=>$tly));
            $this->set(compact('tyles','tong','namhocs','hockies','namhoc_id','hocky_id'));        
      }else{
            $this->Session->setFlash(__('Invalid! Khong thuoc khoa cua ban', true));
        }
    }
    
    function tiletbhk($namhoc_id=0,$hocky_id = 0)
    {
    	$khoaht_id=$this->Session->read('khoa_id');
        $chidoanht=$this->Session->read('chidoanht_id');
        $info=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$chidoanht),'recursive'=>-1));
        if(!empty($this->data))
        {
        	$namhoc_id =$this->data['Phanloaisv']['namhoc'] ;
        	$hocky_id =$this->data['Phanloaisv']['hocky'] ;
        	$this->redirect(array('action'=>'tiletbhk',$namhoc_id,$hocky_id));
        }
        if($namhoc_id==0)
			$namhoc_id = $this->Session->read('namhocht_id');
        if($hocky_id==0)
        {
        	$hk=$this->getHocKyHt();
        	$hocky_id = $hk['Hocky']['id'];
        }
        if($khoaht_id==$info['Chidoansv']['khoa_id']){
            $this->Z5plsv->recursive = -1;
            $phanloaisvs=$this->Z5plsv->find('all',array('conditions'=>array('hocky_id'=>$hocky_id)));
            $tb=0;$y=0;$xs=0;$k=0;$g=0;
            foreach($phanloaisvs as $phanloaisv){
                if($phanloaisv['Z5plsv']['diemtbhk']<=2.0)$y++;
                 else if($phanloaisv['Z5plsv']['diemtbhk']<=2.5) $k++;
                 else if($phanloaisv['Z5plsv']['diemtbhk']<=3.2) $g++;
                 else $xs++;
            }
            $tong=count($phanloaisvs);
            if($tong>0){
                $tlxs=round(($xs/$tong),2)*100;
                $tlk=round(($k/$tong),2)*100;
                $tltb=round(($tb/$tong),2)*100;
                $tly=round(($y/$tong),2)*100;
                $tlg=round(($g/$tong),2)*100;      
            }else {
                $tlxs=0;
                $tlk=0;
                $tltb=0;
                $tly=0;
                $tlg=0;
            }
            $this->Namhoc->displayField='namhoc';
    		$namhocs = $this->Namhoc->find('list');
    		$hockies = $this->Hocky->find('list',array('conditions'=>array('namhoc_id'=>$namhoc_id),'fields'=>array('tenhocky')));
            $tyles = array(array('loai'=>'Xuất sắc','tyle'=>$tlxs),array('loai'=>'Giỏi','tyle'=>$tlg),array('loai'=>'Khá','tyle'=>$tlk),array('loai'=>'Trung bình','tyle'=>$tltb),array('loai'=>'Yếu','tyle'=>$tly));
            $this->set(compact('tyles','tong','namhocs','hockies','namhoc_id','hocky_id'));
      }else{
            $this->Session->setFlash(__('Invalid! Khong thuoc khoa cua ban', true));
        }
    }
    
     
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid phanloaisv', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('phanloaisv', $this->Phanloaisv->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Phanloaisv->create();
			if ($this->Phanloaisv->save($this->data)) {
				$this->Session->setFlash(__('The phanloaisv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The phanloaisv could not be saved. Please, try again.', true));
			}
		}
		$xeploais = $this->Phanloaisv->Xeploai->find('list');
		$sinhviens = $this->Phanloaisv->Sinhvien->find('list');
		$hockies = $this->Phanloaisv->Hocky->find('list');
		$this->set(compact('xeploais', 'sinhviens', 'hockies'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid phanloaisv', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Phanloaisv->save($this->data)) {
				$this->Session->setFlash(__('The phanloaisv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The phanloaisv could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Phanloaisv->read(null, $id);
		}
		$xeploais = $this->Phanloaisv->Xeploai->find('list');
		$sinhviens = $this->Phanloaisv->Sinhvien->find('list');
		$hockies = $this->Phanloaisv->Hocky->find('list');
		$this->set(compact('xeploais', 'sinhviens', 'hockies'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for phanloaisv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Phanloaisv->delete($id)) {
			$this->Session->setFlash(__('Phanloaisv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Phanloaisv was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function updatehocky()
	{
		$this->layout='ajax';
		$namhoc_id = $this->data['Phanloaisv']['namhoc'];
		$hockies = $this->Hocky->find('list',array('conditions'=>array('namhoc_id'=>$namhoc_id),'fields'=>array('tenhocky')));
		$this->set(compact('hockies'));
	}
}
